package projorientado.ocr.vfeature.impl;

import java.awt.Dimension;
import projorientado.ocr.vfeature.*;
import projorientado.image.ImageLayerData;

public class ImagePairExtractor implements FeaturesPairExtractor {

    private FeaturesExtractor extractor;
    private int sizeMode;
    public static final int MAX_SIZE = 0,  MIN_SIZE = 1,  AVG_SIZE = 2;

    public ImagePairExtractor() {
        super();
    }

    public ImagePairExtractor(FeaturesExtractor extractor, int sizeMode) {
        super();
        this.extractor = extractor;
        this.sizeMode = sizeMode;
    }

    public FeaturesExtractor getExtractor() {
        return extractor;
    }

    public void setExtractor(FeaturesExtractor extractor) {
        this.extractor = extractor;
    }

    public FeaturesPair extractFeaturesPair(ImageLayerData image1, ImageLayerData image2) {

        Dimension d = getSize(image1, image2);

        float diffFactor = Math.abs((image1.getWidth() * image1.getHeight()) - (image2.getWidth() * image2.getHeight()));

        diffFactor /= d.width * d.height;
        diffFactor = 1 - diffFactor;

        SequentialFeatures s1 = extractor.extractFeatures(image1, d.width, d.height);
        SequentialFeatures s2 = extractor.extractFeatures(image2, d.width, d.height);
        return new FeaturesPair(s1, s2, diffFactor);
    }

    private Dimension getSize(ImageLayerData image1, ImageLayerData image2) {
        Dimension d = new Dimension();

        if (sizeMode == MAX_SIZE) {
            d.width = Math.max(image1.getWidth(), image2.getWidth());
            d.height = Math.max(image1.getHeight(), image2.getHeight());
        } else if (sizeMode == MIN_SIZE) {
            d.width = Math.min(image1.getWidth(), image2.getWidth());
            d.height = Math.min(image1.getHeight(), image2.getHeight());
        } else if (sizeMode == AVG_SIZE) {
            d.width = Math.abs(image1.getWidth() + image2.getWidth()) / 2;
            d.height = Math.abs(image1.getHeight() + image2.getHeight()) / 2;
        }

        return d;
    }

    public int getSizeMode() {
        return sizeMode;
    }

    public void setSizeMode(int sizeMode) {
        this.sizeMode = sizeMode;
    }
}
